/*
 *  Blazon: A user datagram protocol re-braodcasting daemon.
 *  match.c: IP address and mask handling functions.
 *
 *  Copyright 2012 Blazon development team
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Code derived from ircd-ratbox.
 *
 *  Copyright (C) 1990 Jarkko Oikarinen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "std-include.h"

#include <netinet/in.h>	/* inet_ntop() */
#include <arpa/inet.h>	/* inet_ntop() */
#include <netinet/ip.h>	/* IP headers */
#include <sys/types.h>
#include <sys/socket.h>

bool valid_ip_address(const char *);
bool valid_cidr_mask(const char *);
bool comp_with_mask(void *, void *, unsigned int);
bool match_ip(const char *, const char *);


bool
valid_ip_address(const char *address)
{
    struct sockaddr_storage sa;
	int aftype;
	void *ip_ptr;

	if(strchr(address, ':'))
	{
		aftype = AF_INET6;
		ip_ptr = &((struct sockaddr_in6 *)&sa)->sin6_addr;
	}
	else if(!strchr(address, ':'))
	{
		aftype = AF_INET;
		ip_ptr = &((struct sockaddr_in *)&sa)->sin_addr;
	}
	else
		return false;

	if (inet_pton(aftype, address, ip_ptr) == 1)
		return true;
	else
		return false;
}

bool
valid_cidr_mask(const char *cidr)
{
	char address[INET_ADDRSTRLEN+1];
	char *cptr;
	int cidrlen;

	strlcpy(address, cidr, sizeof(address));

	cptr = strrchr(address, '/');
	if(cptr == NULL)
		return false;

	/* Replace the '/' with the str terminator so inet_pton()
	 * in valid_ip_address() doesn't get confused by the mask.
	 */
	*cptr++ = '\0';

	cidrlen = atoi(cptr);
	/* maybe check bounds for cidrlen? */
	if(cidrlen < 1 || cidrlen > 128)
		return false;

	if(valid_ip_address(address))
		return true;
	else
		return false;
}


bool
comp_with_mask(void *addr, void *dest, unsigned int mask)
{
	if(memcmp(addr, dest, mask / 8) == 0)
	{
		int n = mask / 8;
		int m = ((-1) << (8 - (mask % 8)));
		if(mask % 8 == 0 || (((uint8_t *)addr)[n] & m) == (((uint8_t *)dest)[n] & m))
			return true;
	}
	return false;
}



/* match_ip() - Matches an IP against a CIDR mask.
 *
 * Input - cidr ip mask, ip address
 */
bool
match_ip(const char *s1, const char *s2)
{
	struct sockaddr_storage ipaddr, maskaddr;
	char address[INET_ADDRSTRLEN+1];
	char mask[INET_ADDRSTRLEN+4];
	char *len;
	void *ip_ptr, *mask_ptr;
	int cidrlen, aftype;

	strlcpy(address, s1, sizeof(address));
	strlcpy(mask, s2, sizeof(mask));

	len = strrchr(mask, '/');
	if(len == NULL)
		return false;

	*len++ = '\0';

	cidrlen = atoi(len);
	if(cidrlen <= 0)
		return false;

	/* This handles an IPv6 address and mask */
	if(strchr(mask, ':') && strchr(address, ':'))
	{
		if (cidrlen > 128)
			return false;

		aftype = AF_INET6;
		ip_ptr = &((struct sockaddr_in6 *)&ipaddr)->sin6_addr;
		mask_ptr = &((struct sockaddr_in6 *)&maskaddr)->sin6_addr;
	}
	else if(!strchr(mask, ':') && !strchr(address, ':'))
	{
		if (cidrlen > 32)
			return false;

		aftype = AF_INET;
		ip_ptr = &((struct sockaddr_in *)&ipaddr)->sin_addr;
		mask_ptr = &((struct sockaddr_in *)&maskaddr)->sin_addr;
	}
	else
		return false;

	inet_pton(aftype, address, ip_ptr);
	inet_pton(aftype, mask, mask_ptr);
	if(comp_with_mask(ip_ptr, mask_ptr, cidrlen))
		return true;
	else
		return false;
}
